<?php
namespace app\demo_record\controller;

use cmf\controller\HomeBaseController;
use app\demo_record\model\RecordModel;
use think\Db;
use app\demo_record\widget\ListWidget;


class RecordController extends HomeBaseController
{
    public function showCreateForm()
    {
        $cate_controller=new CategoryController();
        $category_options=$cate_controller->fetchTreeSelectOptions();
        $this->assign("category_options",$category_options);
        return $this->fetch("create_form");
    }
    public function insertItem()
    {
        $data=input();
        $model=new RecordModel();
        $current_time=time();
        $override=[
            "user_id"=>cmf_get_current_user_id(),
            "is_check"=>0,
            "create_time"=>$current_time,
            "update_time"=>$current_time
        ];
        $data=array_merge($data,$override);
        $id=$model->db()->insertGetId($data);
        if(!$id){
            $this->error($model->getError());
        }
        $this->success("插入成功，id=$id");
    }
    public function showEditForm()
    {
        $id=input("id","");
        if( $id==="" ){
            $this->error("记录编号不能为空");
        }
        $item=RecordModel::get($id);
        $this->assign("item",$item);
        $cate_controller=new CategoryController();
        $category_options=$cate_controller->fetchTreeSelectOptions($item["category_id"]);
        $this->assign("category_options",$category_options);

        // $category=CategoryModel::get($item["category_id"]);
        // $this->assign("category",$category);
        return $this->fetch("edit_form");
    }
    public function updateItem()
    {
        $data=input();
        if( !isset($data["id"]) || $data["id"]==="" ){
            $this->error("记录编号不能为空");
        }
        else{
            $id=$data["id"];
            unset($data["id"]);
        }

        $override=[
            "user_id"=>cmf_get_current_user_id(),
            "is_check"=>0,
            "update_time"=>time()
        ];
        $data=array_merge($data,$override);

        $model=RecordModel::update($data,["id"=>$id]);
        if($model===false){
            $this->error("更新失败");
        }
        $this->success("更新成功");
    }
    public function showMineList()
    {
        $user_id=cmf_get_current_user_id();
        if(!$user_id){
            $this->error("请先登录","user/login/index");
        }
        $model=new RecordModel();
        $sub_sql="SELECT `title` FROM `dm_demo_category` AS `category` WHERE `category`.`id` = `record`.`category_id`";
        $list=$model->db()->alias("record")->field("*,($sub_sql) AS category_title")->where(["user_id"=>$user_id])->paginate(20);
        $page=$list->render();


        $list_widget=new ListWidget();
        $list_widget->setFields([
            "id"=>["title"=>"编号","width"=>50],
            "user_id"=>["title"=>"创建者","type"=>"refer","table"=>"cmf_user","id_field"=>"id","title_field"=>"name"],
            "category_title"=>["title"=>"分类"],
            "message"=>["title"=>"消息"],
            "is_check"=>["title"=>"审核","type"=>"select","options"=>["0"=>"未审核","1"=>"通过","2"=>"拒绝"]],
            "create_time"=>["title"=>"创建时间","type"=>"datetime","format"=>"Y-m-d H:i:s"],
            "update_time"=>["title"=>"更新时间","type"=>"datetime","format"=>"Y-m-d H:i:s"],
        ]);
        $list_widget->list=$list;
        $list_widget->setItemActionCallback(function($item){
            return [
                ["title"=>"编辑","url"=>url('showEditForm',['id'=>$item['id']])],
                ["title"=>"删除","url"=>url('deleteItem',['id'=>$item['id']]),"type"=>"ajax","flag"=>"delete"]
            ];
        });
        $list_html = $list_widget->fetchTableWidget();
        echo $list_html;
        exit;

        $is_check_arr=[
            "0"=>"未审核",
            "1"=>"通过",
            "2"=>"拒绝",
        ];
        foreach($list as $index => $item){
            $item["is_check_str"] = isset($is_check_arr[$item["is_check"]]) ? $is_check_arr[$item["is_check"]] : "";
            $item["create_time_str"]=date("Y-m-d H:i:s",$item["create_time"]);
            $item["update_time_str"]=date("Y-m-d H:i:s",$item["update_time"]);
        }
        $this->assign("list",$list);
        $this->assign("page",$page);
        return $this->fetch("mine_list").$list_html;
    }
    public function deleteItem()
    {
        $id=input("id","");
        if( $id==="" ){
            $this->error("记录编号不能为空");
        }
        $count=RecordModel::destroy($id);
        if(!$count){
            $this->error("删除失败");
        }
        $this->success("操作成功");
        
        // $model=new CategoryModel();
        // $res=$model->db()->where(["id"=>$id])->delete();
        // if($res===false){
        //     $this->error("删除失败");
        // }
        // $this->success("操作成功");
    }
}
